package bancosys.tec.persist.persister;

import org.hibernate.event.PostLoadEvent;
import org.hibernate.event.PostLoadEventListener;

import bancosys.tec.component.ControllerLocator;
import bancosys.tec.persist.Persistable;
import bancosys.tec.persist.controller.PersistenceEnabledController;

/**
 * @created Feb 6, 2007
 * @author Gustavo Almeida
 */
public class PersisterPostLoadEventListener implements PostLoadEventListener {
    private ControllerLocator controllerLocator = null;

    /**
     * Injeta o controller no bean.
     * 
     * @param event evento de PostLoad
     */
    public void onPostLoad(PostLoadEvent event) {
        Object entity = event.getEntity();
        if (entity instanceof Persistable) {
            Persistable bean = (Persistable) entity;
            bean.setController((PersistenceEnabledController) this.controllerLocator.findControllerForClass(bean.getClass()));
        }
    }

    /**
     * Define o controllerLocator que será usado para localizar os controller dos beans.
     * 
     * @param controllerLocator O controllerLocator a ser definido.
     */
    public void setControllerLocator(ControllerLocator controllerLocator) {
        this.controllerLocator = controllerLocator;
    }
}
